<template>
  <div class="goods">
    <div class="menu-wrapper" ref="menuWrapper">
      <ul>
        <li class="menu-item border-1px" v-for="(item,index) in goods" :class="{'current':currentIndex === index}"
            @click="selectMenu(index,$event)">
          <span class="text">
            <span class="icon" :class="classMap[item.type]" v-show="item.type>0"></span>{{item.name}}
          </span>
        </li>
      </ul>
    </div>
    <div class="foods-wrapper" ref="foodsWrapper">
      <ul>
        <li class="foods-list foods-list-hook" v-for="item in goods">
          <h1 class="title">{{item.name}}</h1>
          <ul>
            <li class="foods-item border-1px" v-for="food in item.foods" @click="showFoodDetail(food,$event)">
              <div class="foodIcon">
                <img :src="food.icon" width="56">
              </div>
              <div class="content">
                <div class="name">{{food.name}}</div>
                <div class="desc">{{food.description}}</div>
                <div class="extra">
                  <span class="sellCount">月售{{food.sellCount}}份</span><span class="rating">好评率{{food.rating}}%</span>
                </div>
                <div class="price">
                  <span class="now">￥{{food.price}}</span>
                  <span class="old" v-show="food.oldPrice">￥{{food.oldPrice}}</span>
                </div>
              </div>
              <div class="cartcontrolWrapper">
                <cartcontrol :food="food" :select-foods="selectFoods"></cartcontrol>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <div class="cart-wrapper">
      <cart :deliveryPrice="seller.deliveryPrice" :minPrice="seller.minPrice" :select-foods="selectFoods"></cart>
    </div>
    <fooddetail :food="clickedFood" ref="fooddetail"></fooddetail>

  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll'
  import cart from '../../components/cart/cart.vue'
  import cartcontrol from '../../components/cartcontrol/cartcontrol.vue'
  import fooddetail from '../../components/fooddetail/fooddetail.vue'
  const ERR_OK = 0
  export default {
    props: {
      seller: {
        type: Object
      }
    },
    data(){
      return {
        goods: [],
        listHeight: [],
        scrollY: 0,
        clickedFood: {}
      }
    },
    created(){
      this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']
      this.$http.get('/api/goods').then((response) => {
        //get body data
        response = response.body
        if (response.errno === ERR_OK) {
          this.goods = response.data
          this.$nextTick(() => {
            this._initScroll()
            this.calculateHeight()
          })
        }
      })
    },
    components: {
      cart,
      cartcontrol,
      fooddetail
    },
    computed: {
      currentIndex(){
        for (let i = 0; i < this.listHeight.length; i++) {
          let height1 = this.listHeight[i]
          let height2 = this.listHeight[i + 1]
          if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
            return i
          }
        }
        return 0
      },
      selectFoods(){
        let foods = []
        this.goods.forEach((good) => {
          good.foods.forEach((food) => {
            if (food.count) {
              foods.push(food)
            }
          })
        })
        return foods
      }
    },
    methods: {
      _initScroll(){
        this.menuWrapper = new BScroll(this.$refs.menuWrapper, {
          click: true
        });
        this.foodsWrapper = new BScroll(this.$refs.foodsWrapper, {
          probeType: 3,
          click: true
        });
        this.foodsWrapper.on('scroll', (pos) => {
          this.scrollY = Math.abs(Math.round(pos.y))
        })
      },
      calculateHeight(){
        let height = 0;
        this.listHeight.push(height);
        let foodsList = this.$refs.foodsWrapper.getElementsByClassName('foods-list-hook')
        for (let i = 0; i < foodsList.length; i++) {
          let itemHeight = foodsList[i].clientHeight
          height += itemHeight
          this.listHeight.push(height);
        }
      },
      selectMenu(index, event){
        if (!event._constructed) {//过滤掉原生的js点击事件（原生的js点击事件没有_constructed属性）
          return
        }
        let foodsList = this.$refs.foodsWrapper.getElementsByClassName('foods-list-hook')
        let el = foodsList[index]
        this.foodsWrapper.scrollToElement(el, 300)
      },
      showFoodDetail(food,event){
          if(!event._constructed){
              return
          }
          this.clickedFood = food
          this.$refs.fooddetail.show()
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/mixin.styl"
  .goods
    display: flex
    position: absolute
    top: 174px
    bottom: 58px
    overflow: hidden
    width: 100%
    .menu-wrapper
      flex: 0 0 80px
      width: 80px
      .menu-item
        display: table
        padding: 0 12px
        width: 56px
        height: 54px
        background: #f3f5f7
        &:last-child
          .text
            border-none()
        &.current
          background: #fff
          .text
            font-weight: 700
            border-none()
        .text
          display: table-cell
          vertical-align: middle
          border-1px(rgba(7, 17, 27, 0.1))
          font-size: 12px
          line-height: 14px
          .icon
            display: inline-block
            margin-right: 2px
            width: 12px
            height: 12px
            vertical-align: top
            background-size: 100%
            background-repeat: no-repeat
            &.decrease
              bg-image("decrease_3")
            &.discount
              bg-image("discount_3")
            &.guarantee
              bg-image("guarantee_3")
            &.invoice
              bg-image("invoice_3")
            &.special
              bg-image("special_3")
    .foods-wrapper
      flex: 1
      .title
        height: 26px
        padding-left: 12px
        border-left: 2px solid #d9dde1
        background: #f3f5f7
        line-height: 26px
        color: rgb(147, 153, 159)
        font-size: 12px
      .foods-item
        display: flex
        margin: 18px
        padding-bottom: 18px
        border-1px(rgba(7, 17, 27, 0.1))
        &:last-child
          border-none()
          margin-bottom: 0
        .foodIcon
          flex: 0 0 57px
          margin-right: 10px
        .content
          flex: 1
          .name
            margin: 2px 0 8px
            color: rgb(7, 17, 27)
            font-size: 14px
          .desc, .extra
            color: rgb(147, 153, 159)
            font-size: 10px
          .desc
            margin-bottom: 8px
            line-height: 12px
          .extra
            .sellCount
              margin-right: 12px
          .price
            line-height: 24px
            .now
              margin-right: 8px
              color: rgb(240, 20, 20)
              font-weight: bold
              font-size: 14px
            .old
              text-decoration: line-through
              color: rgb(147, 153, 159)
              font-weight: bold
              font-size: 10px

        .cartcontrolWrapper
          position: absolute
          right: 0
          bottom: 12px
    .cart-wrapper
      position: fixed
      bottom: 0
      left: 0
      z-index: 50
      width: 100%
      height: 48px
</style>
